using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CM_SusiePianoAnimation : MonoBehaviour
{
	[Serializable]
	public class CM_SusieKeyPress
	{
		public int Key;

		public float TimeSinceLast;

		public CM_SusieKeyPress(int key, float timeSinceLast)
		{
			Key = key;
			TimeSinceLast = timeSinceLast;
		}
	}

	public List<CM_SusieKeyPress> keyPresses = new List<CM_SusieKeyPress>();

	private float lastPressTime;

	[SerializeField]
	private Animator Susie;

	private void Update()
	{
		if (DRHDebugManager.instance.DebugModeEnabled)
		{
			if (Input.GetKeyDown(KeyCode.Q))
			{
				RecordKey(1);
			}
			else if (Input.GetKeyDown(KeyCode.W))
			{
				RecordKey(2);
			}
			else if (Input.GetKeyDown(KeyCode.E))
			{
				RecordKey(3);
			}
			else if (Input.GetKeyDown(KeyCode.R))
			{
				RecordKey(4);
			}
			if (Input.GetKeyDown(KeyCode.Space))
			{
				StartCoroutine(Playback());
			}
		}
	}

	public void PlayRecording()
	{
		StartCoroutine(Playback());
	}

	private void RecordKey(int key)
	{
		float time = Time.time;
		float num = ((keyPresses.Count == 0) ? 0f : (time - lastPressTime));
		lastPressTime = time;
		keyPresses.Add(new CM_SusieKeyPress(key, num));
		Debug.Log($"Recorded key {key} with time since last: {num}");
	}

	private IEnumerator Playback()
	{
		Debug.Log("Starting playback...");
		foreach (CM_SusieKeyPress keyPress in keyPresses)
		{
			yield return new WaitForSeconds(keyPress.TimeSinceLast);
			Susie.Play($"SusieDarkworld_CM_KeyPress{keyPress.Key}", -1, 0f);
			Debug.Log($"Key: {keyPress.Key}");
		}
		Debug.Log("Playback finished.");
	}
}
